import {
  API
} from "../../utils/const"
import {
  checkCount
} from "../../utils/formCheckUtil";
import {
  getRequest,
  postRequest
} from "../../utils/request"
const app=getApp();
// pages/equipIndex/home.js.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    app:app,
    showData: [],
    equipStatus: [],
    searchStr: '',
    tableData: [],
    showEmpty: false,
    showList: ['可领用', "已领用", "已污染", "消毒中"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onCellClick(e) {
    console.log("e", e)
    const dataItem = e.currentTarget.dataset.item;
    console.log("dataItem", dataItem)
    const {
      equipName,
      status,
      value
    } = dataItem;
    if (status && value > 0) {
      // 查询明细
      const queryDetailParams = {
        equipName,
        equipStatus: status
      }
      console.log("queryDetailParams", queryDetailParams)
      this.fetchDetailList(queryDetailParams)
    }
  },
  bindSearchInput(e) {
    console.log(e);
    var val = e.detail.value.trim();
    this.setData({
      searchStr: val
    })
    this.fetchEquipInfo(val)
  },
  fetchDetailList(queryParams) {
    getRequest(API.instanceList, queryParams).then(res => {
      console.log("result", res);
      this.setData({
        total: res.total,
        size: res.size,
        current: res.current,
        equipList: res.records
      })
    })
  },
  makeTableData(showData, equipStatus) {
    const totalData = [];
    const titleRow = [];
    // const equipStatus = this.data.equipStatus;
    titleRow.push("器械名称")
    console.log("this.data.equipStatus", equipStatus)
    equipStatus.forEach(element => {
      // if(element.text!=='已丢失')
      titleRow.push(element.text);
    });
    totalData.push(titleRow)

    for (let key in showData) {
      const dataRow = [];
      dataRow.push(key);
      // const statusData = dataRow[key];
      for (let i = 0; i < equipStatus.length; i++) {
        if (showData[key][equipStatus[i].name] && showData[key][equipStatus[i].name] !== '已丢失') {
          // const data=
          dataRow.push({
            value: showData[key][equipStatus[i].name],
            status: equipStatus[i].name,
            equipName: key
          });
        } else {
          dataRow.push({
            value: 0,
            status: equipStatus[i].name,
            equipName: key
          });
        }
      }
      totalData.push(dataRow)
    }
    console.log("totalData", totalData)
    this.setData({
      tableData: totalData

    })
  },
  fetchAllEquipStatus() {
    getRequest(API.equipStatus).then(res => {
      this.setData({
        equipStatus: res
      })
    })
  },
  onEquipClick: function (e) {
    // console.
    const {
      equipName
    } = e.currentTarget.dataset.value;
    // getRequest("", {
    //   equipName: item.equipName
    // }).then(res => {

    // })
    wx.navigateTo({
      url: '/pages/usingHistory/usingHistory?equipName=' + equipName,
    })
  },
  onLongTapEquip: function (e) {
    console.log(this.equipStatus)
    const that = this;
    const item = e.currentTarget.dataset.value;
    console.log("item", item)
    const maxEnable = item['可领用'];
    wx.showModal({
      title: `正在领用器械：${item.equipName}`,
      editable: true,
      confirmText: '领用',
      placeholderText: `输入领用数量,最多可领用${maxEnable}个`,
      // content:1
      success: (res) => {
        if (res.confirm) {

          const count = checkCount(res.content)
          if (count > 0) {
            const params = {
              count,
              type: "USE",
              equipName: item.equipName
            }
            that.equipUse(params).then(res => {
              that.fetchEquipInfo(that.data.searchStr);
            });
          } else {
            that.fetchEquipInfo(that.data.searchStr);
          }

        }
      }
    })
  },
  equipUse(params) {
    return postRequest(API.equipChange, {
      ...params
    }).then(res => {
      wx.hideLoading();
      if (res) {
        wx.showToast({
          title: '领用成功',
          icon: 'success',
          duration: 1000,
        })
      }
    })
  },
  fetchEquipInfo(searchStr) {

    getRequest(API.instanceTotalGroup, searchStr ? {
      searchStr
    } : {}).then(res => {
      getRequest(API.equipStatus).then(eqStaus => {
        // this.makeTableData(res, eqStaus);
        const datas = [];
        console.log("res", res)
        Object.keys(res).forEach(key => {
          console.log("key", key)
          const itemData = {
            equipName: key,
          }
          const statusData = res[key];
          console.log("datas", statusData)
          const statusList = this.data.showList;
          statusList.forEach(show => {
            if (!statusData[show]) {
              itemData[show] = 0
            } else {
              itemData[show] = statusData[show]
            }
          })
          datas.push(itemData);

        })
        console.log("datas", datas)
        this.setData({
          equipList: datas,
          equipStatus: eqStaus,
          showEmpty: datas.length <= 0
        })
      })

      this.setData({
        showData: res
      })
    })
  },
  doListQuery(e) {
    // console.log("e",e);
    // console.log("queryPa",queryParams)
    getRequest(API.instanceTotalGroup,e.detail).then(res => {
      getRequest(API.equipStatus).then(eqStaus => {
        // this.makeTableData(res, eqStaus);
        const datas = [];
        // console.log("res", res)
        Object.keys(res).forEach(key => {
          // console.log("key", key)
          const itemData = {
            equipName: key,
          }
          const statusData = res[key];
          // console.log("datas", statusData)
          const statusList = this.data.showList;
          statusList.forEach(show => {
            if (!statusData[show]) {
              itemData[show] = 0
            } else {
              itemData[show] = statusData[show]
            }
          })
          datas.push(itemData);

        })
        console.log("datas", datas)
        this.setData({
          equipList: datas,
          equipStatus: eqStaus,
          showEmpty: datas.length <= 0
        })
      })

      this.setData({
        showData: res
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      searchStr: null
    })
    this.fetchAllEquipStatus();
    this.fetchEquipInfo();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})